//
//  YDContactDetailViewController.m
//  IngCard
//
//  Created by kf on 2017/10/13.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDContactDetailViewController.h"
#import "YDContactHeaderView.h"
#import "YDContactDetailSectionHeaderView.h"
#import "YDContactExperienceCell.h"
#import "YDContactRecentMeetsCell.h"
#import "YDTextViewKeyboardViewController.h"

@interface YDContactDetailViewController ()
@property(nonatomic, strong) YDTextViewKeyboardViewController *textViewKeyboardViewController;
@end

@implementation YDContactDetailViewController

-(void) viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = false;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [THEME renderStatusBarStyle:YDStatusBarStyle_Light];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


-(void)initSubviews
{
    [super initSubviews];
    
    YDContactHeaderView *headerView = [YDContactHeaderView new];
    headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 249);
    headerView.back = ^{
        [self.navigationController popViewControllerAnimated:YES];
    };
    
    self.tableView.tableHeaderView = headerView;
    UIEdgeInsets edge = UIEdgeInsetsMake(-20, 0, 0, 0);
    [self.tableView setContentInset:edge];
}

-(void)initDataSource
{
    Weak(self);
    [self.dataSource addDataSource:[YDContactExperienceCellModel class] atSection:0
                            config:^(YDContactExperienceCellModel *dataSource)
    {
        dataSource.occupation = @"UI&UX designer";
        dataSource.company = @"YiTu Technology";
        dataSource.timer = @"July 2017-Now（Two months)";
    }];
    
    [self.dataSource addDataSource:[YDContactExperienceCellModel class] atSection:1
                            config:^(YDContactExperienceCellModel * dataSource)
    {
        dataSource.occupation = @"Academy of Arts & Desigh,Tsinghua University";
        dataSource.company = @"MS,Information art design";
        dataSource.timer = @"2014-2017";
        dataSource.image = @"Contact_detail_thumbnail";
    }];
    
    [self.dataSource addDataSource:[YDContactExperienceCellModel class] atSection:1
                            config:^(YDContactExperienceCellModel * dataSource)
     {
         dataSource.occupation = @"Academy of Arts & Desigh,Tsinghua University";
         dataSource.company = @"MS,Information art design";
         dataSource.timer = @"2010-2014";
         dataSource.image = @"Contact_detail_thumbnail";
     }];
    
    [self.dataSource addDataSource:[YDContactRecentMeetsCellModel class] atSection:2
                            config:^(YDContactRecentMeetsCellModel *dataSource)
     {
         dataSource.date = @"Saturday. Augest 21";
         dataSource.timing = @"2:20 PM";
         dataSource.content = @"“These guys may be future partner of me. And I would like to Impress them";
         
         Weak(dataSource);
         dataSource.edit = ^{
             Strong(self);
             Strong(dataSource);
             [self editContent:dataSource];
         };
     }];
    
    [self.dataSource addDataSource:[YDContactRecentMeetsCellModel class] atSection:2
                            config:^(YDContactRecentMeetsCellModel *dataSource)
     {
         dataSource.date = @"Friday. March";
         dataSource.timing = @"10:20 AM";
         
         Weak(dataSource);
         dataSource.edit = ^{
             Strong(self);
             Strong(dataSource);
             [self editContent:dataSource];
         };
    }];
    
    [self.dataSource addDataSource:[YDContactRecentMeetsCellModel class] atSection:2
                            config:^(YDContactRecentMeetsCellModel *dataSource)
     {
         dataSource.date = @"Friday. March";
         dataSource.timing = @"10:20 AM";
         
         Weak(dataSource);
         dataSource.edit = ^{
             Strong(self);
             Strong(dataSource);
             [self editContent:dataSource];
         };
     }];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YDContactDetailSectionHeaderView *view = [YDContactDetailSectionHeaderView new];
    
    switch (section) {
        case 0:
            {
                view.title = @"Experience";
                view.onMore = ^{
                    [QMUITips showInfo:@"进入列表页" inView:self.view hideAfterDelay:2];
                };
            }
            break;
        case 1:
        {
            view.title = @"Education";
            view.onMore = ^{
                [QMUITips showInfo:@"进入列表页" inView:self.view hideAfterDelay:2];
            };
        }
            break;
        case 2:
        {
            view.title = @"Recent meets";
            view.onMore = ^{
                [QMUITips showInfo:@"进入列表页" inView:self.view hideAfterDelay:2];
            };
        }
            break;
        default:
            break;
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0f;
}

#pragma mark - private

-(void) editContent:(YDContactRecentMeetsCellModel*) cellModel
{
    NSString *placehold = cellModel.content ?: @"请输出备注";
    
    [self.textViewKeyboardViewController showWithPlaceholder:placehold onComplete:^(NSString *value)
     {
         cellModel.content = value;
         [self.dataSource reloadDataSource:cellModel];
         [self.tableView layoutIfNeeded];
     } onCancel:^
     {
         Log(@"cancel");
     }];
    
}

#pragma mark - lazy load
-(YDTextViewKeyboardViewController *)textViewKeyboardViewController
{
    if (!_textViewKeyboardViewController)
    {
        _textViewKeyboardViewController = [[YDTextViewKeyboardViewController alloc] init];
        _textViewKeyboardViewController.parendViewController = self;
    }
    return _textViewKeyboardViewController;
}

@end
